﻿using System;
using System.Web.UI.WebControls;
using METU_CMT.Business;

namespace METU_CMT
{
    public partial class EditTeachingAssistantsScreen : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTeachingAssistantsHaveAccounts();
                BindTeachingAssistantsOfCurrentCourse();
            }
        }

        protected void OnBtnAddClicked(object sender, EventArgs e)
        {
            Instructor _instructor = (Instructor)EntityFactory.User;
            string _password = CommonMethods.CreateRandomPassword(6);
            //TODO: Must send an email for giving password to TeachingAssistant.
            _instructor.AddNewUser( txtNewTeachingAssistantName.Text, txtNewTeachingAssistantSurName.Text,
                                    txtNewTeachingAssistantUserName.Text,
                                    _password, txtNewTeachingAssistantEmail.Text, UserType.TA, DateTime.Now.AddYears(2));

            BindTeachingAssistantsHaveAccounts();
        }

        private void BindTeachingAssistantsHaveAccounts()
        {
            dtgTeachingAssistantsHavingAccounts.DataSource = EntityFactory.GetAllTeachingAssistants();
            dtgTeachingAssistantsHavingAccounts.DataBind();
        }

        protected void OnBtnPullFromMetuOnlineClicked(object sender, EventArgs e)
        {

        }

        private void BindTeachingAssistantsOfCurrentCourse()
        {
            dtgTeachingAssistants.DataSource = EntityFactory.GetTeachingAssistantsOfCourse(EntityFactory.CourseId);
            dtgTeachingAssistants.DataBind();
        }

        protected void OnRowDtgTeachingAssistantsHavingAccountsRowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.CompareTo("AddToCourse") == 0)
            {
                int _teachingAssistantId;
                if (int.TryParse(e.CommandArgument.ToString(), out _teachingAssistantId) == false)
                {
                    //TODO: Log TeachingAssistant id alınamadı.
                }
                Instructor _instructor = (Instructor)EntityFactory.User;
                _instructor.AssignTeachingAssistantToCourse(EntityFactory.CourseId, _teachingAssistantId);

                BindTeachingAssistantsOfCurrentCourse();
            }
        }

        protected void OnDtgTeachingAssistantsCancellingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            dtgTeachingAssistants.EditIndex = -1;
            BindTeachingAssistantsOfCurrentCourse();
        }

        protected void OnDtgTeachingAssistantsRowEditing(object sender, GridViewEditEventArgs e)
        {
            dtgTeachingAssistants.EditIndex = e.NewEditIndex;
            BindTeachingAssistantsOfCurrentCourse();
        }

        protected void OnDtgTeachingAssistantsRowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void OnDtgTeachingAssistantsRowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            dtgTeachingAssistants.Rows[e.RowIndex].Focus();
            int _teachingAssistantId = CommonMethods.GetIdFromGridViewRow(dtgTeachingAssistants.Rows[e.RowIndex]);

            TextBox _txtNameEdit = (TextBox)dtgTeachingAssistants.Rows[e.RowIndex].FindControl("txtNameEdit");
            TextBox _txtSurnameEdit = (TextBox)dtgTeachingAssistants.Rows[e.RowIndex].FindControl("txtSurnameEdit");
            TextBox _txtUserNameEdit = (TextBox)dtgTeachingAssistants.Rows[e.RowIndex].FindControl("txtUserNameEdit");
            TextBox _txtEMailEdit = (TextBox)dtgTeachingAssistants.Rows[e.RowIndex].FindControl("txtEMailEdit");

            Instructor _instructor = (Instructor)EntityFactory.User;
            _instructor.UpdateUser( _teachingAssistantId, _txtNameEdit.Text, _txtSurnameEdit.Text, _txtUserNameEdit.Text,
                                    _txtEMailEdit.Text, DateTime.Now.AddYears( 2 ) );

            dtgTeachingAssistants.EditIndex = -1;

            BindTeachingAssistantsOfCurrentCourse();
            BindTeachingAssistantsHaveAccounts();
        }

        protected void OnDtgTeachingAssistantsRowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int _teachingAssistantId = CommonMethods.GetIdFromGridViewRow(dtgTeachingAssistants.Rows[e.RowIndex]);
            Instructor _instructor = (Instructor)EntityFactory.User;
            _instructor.DeleteTeachingAssistantFromCourse(EntityFactory.CourseId, _teachingAssistantId);

            BindTeachingAssistantsOfCurrentCourse();
        }
    }
}